package com.adaxi.adc.business.vehicletrip.filter;

import com.adaxi.adc.common.common.CommonResult;
import com.adaxi.adc.common.constant.Constants;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 涛哥
 * @version 1.0
 */
@Slf4j
@Component
public class GateWayWallIntercepter implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String value = request.getHeader(Constants.GATEWAY_WALL_HEADER_NAME);
        if (Constants.GATEWAY_WALL_HEADER_VALUE.equals(value)) {
            return true;
        }
        response.setStatus(403);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter()
                .write(JSON.toJSONString(CommonResult.FAILURE("禁止翻墙，请走网关")));
        return false;
    }
}
